<?php

class Managenews extends CI_Controller {

    function __construct() {
        parent::__construct();
        $id = $this->session->userdata('id');
        $adminname = $this->session->userdata('adminname');
        $hash = $this->session->userdata('hash');
        
            $this->admin['name'] = "SHYAKEN";
            $this->admin['id'] = 1;
            $this->admin['hash'] = md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE);
            $this->load->model('news_m');
        
        $this->load->model('news_m');
        //$this->load->model('catalogue_m');
    }

    function index() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $this->load->library('pagination');
            $offset = $this->uri->segment(3);
            $limit = 12;
            $config['base_url'] = site_url('admin/managenews');
            $config['uri_segment'] = 3;
            $config['total_rows'] = $this->db->count_all('news');
            $config['per_page'] = $limit;
            $this->pagination->initialize($config);
            $data['paginator'] = $this->pagination->create_links();

            $data['news'] = $this->news_m->getall($limit, $offset, 0);
            //$data['catalogue'] = $this->catalogue_m->get_catalogue();
            $data['option'] = -1;
            $this->load->view('admin/common/header');
            $this->load->view('admin/news', $data);
            $this->load->view('admin/common/footer');
        }else
            redirect(base_url() . 'admin/login');
    }

    function add() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (isset($_POST['add']) && $_POST['add'] == 'Add') {

                $data['title'] = $this->input->post('title');
                $data['slug'] = strtolower(url_title($data['title']));
                $data['content'] = trim($this->input->post("contents"));
                $data['category'] = $this->input->post('catalogue');
                //$data['status'] = $this->input->post('status');
                //$data['author'] = $this->input->post('author');
                $data['special'] = $this->input->post('special');
                $data['image'] = $this->_upload_files();
                if ($data['special'] == '')
                    $data['special'] = 0;
                $data['time'] = time();
                $data['decription'] = $this->input->post('intro');
                $arr = array("\r\n", "\n", "\r");
                $data['content'] = str_replace($arr, '', $data['content']);
                //die(print_r($data));
               // if(!is_numeric(trim($data['order'])))
                 //   $data['order'] = 0;
                if (trim($data['title']) == '' || trim($data['content']) == '' || trim($data['slug']) == '')
                    redirect(base_url() . 'admin/managenews/add');
                $id = $this->news_m->add($data);
                if ($id > 0) {
                    redirect(base_url() . 'admin/managenews');
                }
                else
                    die('Lỗi thêm tin mới!');
            }else {

                $data['catlist'] = $this->news_m->get_cat();

                $this->load->view('admin/common/header');
                $this->load->view('admin/news_add',$data);
                //$this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function update($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id) || $id < 0)
                redirect(base_url() . 'admin/managenews');

            if (isset($_POST['update']) && $_POST['update'] == 'Update') {
                $data['title'] = $this->input->post('title');
                $data['slug'] = strtolower(url_title($data['title']));
                $data['content'] = trim($this->input->post("contents"));
                $data['category'] = $this->input->post('catalogue');
                //$data['status'] = $this->input->post('status');
                //$data['author'] = $this->input->post('author');
                $data['special'] = $this->input->post('special');
                if ($data['special'] == '')
                    $data['special'] = 0;
                $data['time'] = time();
                $data['decription'] = $this->input->post('intro');
                $arr = array("\r\n", "\n", "\r");
                $data['content'] = str_replace($arr, '', $data['content']);
                $thumb = $this->_upload_files();
                if($thumb != "")
                    $data['image'] = $thumb;
                if (trim($data['title']) == '' || trim($data['alias']) == '' || trim($data['content']) == '')
                    redirect(base_url() . 'admin/managenews/update/' . $id);
                
                if ($this->news_m->update($id, $data)) {
                    redirect(base_url() . 'admin/managenews');
                }
                else
                    die('Lỗi cập nhật tin');
            }else {
                //die('xxx');
                $data['news'] = $this->news_m->get_news($id);
                $data['catalogue'] = $this->news_m->get_cat();

                $this->load->view('admin/common/header');
                $this->load->view('admin/news_edit', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function delete($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id))
                redirect(base_url() . 'admin/managenews');
            if ($this->news_m->delete($id)) {
                if (file_exists('uploads/news/' . $id)) {
                    if (file_exists('uploads/news/' . $id . '/thumb.jpg'))
                        unlink('uploads/news/' . $id . '/thumb.jpg');
                    rmdir('uploads/news/' . $id);
                }
                redirect(base_url() . 'admin/managenews');
            }
            else
                die('Lỗi xóa tin');
        }else
            redirect(base_url() . 'admin/login');
    }

    function delete_news() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $ids = $this->input->post('selected');
            if (is_array($ids)) {
                if ($this->news_m->delete($ids))
                    redirect(base_url() . 'admin/managenews');
                else
                    die('Lỗi xóa tin');
            }else {
                redirect(base_url() . 'admin/managenews');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function search() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (isset($_POST['search']) && $_POST['search'] == 'Tìm kiếm') {

                $data['keyword'] = $this->input->post('txt_search');
                $this->session->set_userdata('keyword', $data['keyword']);

                if (trim($data['keyword']) == '')
                    redirect(base_url() . 'admin/managenews');

                $this->load->library('pagination');
                $offset = $this->uri->segment(4);
                $limit = 12;
                $config['base_url'] = site_url('admin/managenews/search');
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->news_m->count_for_search($data['keyword']);
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['paginator'] = $this->pagination->create_links();

                $data['news'] = $this->news_m->search_result($data['keyword'], $limit, $offset);
                $data['option'] = -1;
                
                $this->load->view('admin/common/header');
                $this->load->view('admin/news', $data);
                $this->load->view('admin/common/footer');
            }else {

                $data['keyword'] = $this->session->userdata('keyword');
                if (!isset($data['keyword']) || $data['keyword'] == '')
                    redirect(base_url() . 'admin/managenews');

                $this->load->library('pagination');
                $offset = $this->uri->segment(4);
                $limit = 12;
                $config['base_url'] = site_url('admin/managenews/search');
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->news_m->count_for_search($data['keyword']);
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['paginator'] = $this->pagination->create_links();

                $data['news'] = $this->news_m->search_result($data['keyword'], $limit, $offset);
                $data['option'] = -1;
                
                $this->load->view('admin/common/header');
                $this->load->view('admin/news', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function catalogue() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (isset($_POST['search_catalogue']) && $_POST['search_catalogue'] == 'Danh mục') {

                $data['option'] = $this->input->post('catalogue');
                    $this->session->set_userdata('option', $data['option']);
                $data['order'] = $this->input->post('order');
                    $this->session->set_userdata('order', $data['order']);
                if (trim($data['option']) == ""){
                    $this->session->unset_userdata('option');
                    $this->session->unset_userdata('order');
                    redirect(base_url() . 'admin/managenews');
                }
                    
                //die($data['option']);
                $this->load->library('pagination');
                $offset = $this->uri->segment(4);
                $limit = 12;
                $config['base_url'] = site_url('admin/managenews/catalogue');
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->news_m->count_catalogue($data['option']);
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['paginator'] = $this->pagination->create_links();

                $data['news'] = $this->news_m->get_news_catalogue($data['option'],$data['order'], $limit, $offset);
                $data['catalogue'] = $this->catalogue_m->get_catalogue();
                
                $this->load->view('admin/common/header');
                $this->load->view('admin/news', $data);
                $this->load->view('admin/common/footer');
            }else {
                $data['option'] = $this->session->userdata('option');
                $data['order'] = $this->session->userdata('order');
                if (!isset($data['option']) || $data['option'] == ""){
                    $this->session->unset_userdata('option');
                    $this->session->unset_userdata('order');
                    redirect(base_url() . 'admin/managenews');
                }

                $this->load->library('pagination');
                $offset = $this->uri->segment(4);
                $limit = 12;
                $config['base_url'] = site_url('admin/managenews/catalogue');
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->news_m->count_catalogue($data['option']);
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['paginator'] = $this->pagination->create_links();

                $data['news'] = $this->news_m->get_news_catalogue($data['option'],$data['order'], $limit, $offset);
                $data['catalogue'] = $this->catalogue_m->get_catalogue();
                
                $this->load->view('admin/common/header');
                $this->load->view('admin/news', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }
    
    
    public function _upload_files() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!file_exists('uploads/news/thumbs')) {
                mkdir('uploads/news/thumbs', 0777);
                chmod('uploads/news/thumbs', 0777);
            }
            
                
                $upload_cfg['max_size'] = '2048';
                $upload_cfg['upload_path'] = 'uploads/news';
                $upload_cfg['allowed_types'] = 'gif|jpg|png';
                $upload_cfg['encrypt_name'] = FALSE;
                $upload_cfg['overwrite'] = FALSE;               
                $this->load->library('upload', $upload_cfg);
                
               if($this->upload->do_upload('news_thumb')){ 
                        $img = $this->upload->data();
                        if($this->_thumb($img['full_path'])){    
                          return $img['file_name'];
                        }
                }
            
        } else
            redirect(base_url() . 'admin/login');
    }
    
    function _thumb($img){
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			$config['image_library'] = 'gd2';
			$config['source_image'] = $img;
			$config['new_image'] = 'uploads/news/thumbs';
			$config['cread_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['width'] = 300;
			$config['height'] = 240;
			$this->load->library('image_lib',$config);
			if(!$this->image_lib->resize())
				return false;
			else
				return true;
		
        }else{
            redirect(base_url() . 'admin/login');
        }
    }
}
